Conditions | 5 |
Total Lines | 21 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { DecoratorHelper, pathReducerMethods, toUnderscoreCase } from './utils' |
||
4 | export default function ReducerClass(prefix) { |
||
5 | prefix = toUnderscoreCase(prefix) |
||
6 | |||
7 | return function (target) { |
||
8 | if (DecoratorHelper.isDecorated(target)) { |
||
9 | return target |
||
10 | } |
||
11 | |||
12 | const reducerMap = pathReducerMethods(target, prefix) |
||
13 | const initialState = DecoratorHelper.getStaticField(target, 'initialState') |
||
14 | |||
15 | const reducer = (state = initialState, action) => ( |
||
16 | (action && reducerMap[action.type]) ? reducerMap[action.type](state, action) : state |
||
17 | ) |
||
18 | |||
19 | DecoratorHelper.setStaticMethod(target, '$reducer', reducer) |
||
20 | DecoratorHelper.markAsDecorated(target) |
||
21 | |||
22 | return target |
||
23 | } |
||
24 | } |
||
25 |